/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import collection.Categories;
import collection.Products;
import dao.CategoryDAO;
import dao.ProductDAO;
import dao.UserDAO;
import dto.Category;
import dto.Product;
import dto_pakinfo.User;
import dto_pakinfo.Users;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.stream.XMLStreamException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xml.sax.SAXException;

/**
 *
 * @author duonghung1369
 */
public class LogListener implements ServletContextListener {

    private Log log;

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext context = sce.getServletContext();
        System.setProperty("rootPath", context.getRealPath("/"));

        ProductDAO productDAO = new ProductDAO(Product.class);
        productDAO.openSession();
        List<Product> products = productDAO.all();
        productDAO.closeSession();

        // load users from db and save to users.xml file
        UserDAO userDAO = new UserDAO(User.class);
        userDAO.openSession();
        List<User> users = userDAO.all();
        userDAO.closeSession();

        //load profile from db and save to profiles.xml file
        Products collection = new Products(products);
        Users userCollection = new Users(users);

        String filePath = System.getProperty("rootPath") + util.StringUtil.XML_FOLDER_PATH + "products.xml";
        String userFilePath = System.getProperty("rootPath") + util.StringUtil.XML_FOLDER_PATH + "users.xml";
        String SchemaUserFilePath = System.getProperty("rootPath") + util.StringUtil.XML_FOLDER_PATH + "user.xsd";
        String nonadminUserFilePath = System.getProperty("rootPath") + util.StringUtil.XML_FOLDER_PATH + "nonadminusers.xml";
        log = LogFactory.getLog(LogListener.class);
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(Products.class);
            XMLUtil.marshalObject(jaxbContext, collection, filePath);

            JAXBContext jaxbContextUser = JAXBContext.newInstance(Users.class);
            XMLUtil.marshalObject(jaxbContextUser, userCollection, userFilePath);

            // load categories from db and marshal to categories.xml
            CategoryDAO cateDAO = new CategoryDAO(Category.class);
            cateDAO.openSession();
            List<Category> cateList = cateDAO.all();
            cateDAO.closeSession();

            Categories categoriesObj = new Categories(cateList);
            String categoryFilePath = System.getProperty("rootPath") + util.StringUtil.XML_FOLDER_PATH + "categories.xml";
            jaxbContext = JAXBContext.newInstance(Categories.class);
            XMLUtil.marshalObject(jaxbContext, categoriesObj, categoryFilePath);

            jaxbContext = JAXBContext.newInstance(Users.class);
            Users userUtility = new Users();
            userUtility.getUserIsNotAdmin(userFilePath);

            XMLUtil.marshalObject(jaxbContext, userUtility, nonadminUserFilePath);

        } catch (XMLStreamException ex) {
            Logger.getLogger(LogListener.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(LogListener.class.getName()).log(Level.SEVERE, null, ex);
        } catch (JAXBException ex) {
            log.error("LogListener.contextInitialized()", ex);
        }

    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }
}
